#include <SerialFlash.h>
#include <main.h>
#include "SPI_FLASH.h"

extern volatile bool spi_dma_finish;
void fly_flash_init(void){
	SPI_Flash_Init();
  SPI_DMA_init();
	spi_dma_finish = 0;
	printf("get flash id:%x\n",SPI_FLASH_ReadID());
}
uint32_t fy_flash_readid(void)
{
	return SPI_FLASH_ReadID();
}
void fy_flash_read(uint32_t address24, uint8_t* buffer, uint32_t length)
{
	SPI_FLASH_BufferFastRead(buffer,address24,length);
}

void fy_flash_readdma(uint32_t address24, uint8_t* buffer, uint32_t length)
{
	SPI_FLASH_BufferFastRead(buffer,address24,length);
	//SPI_FLASH_DMARead(address24,buffer,length);
	//SPI_FLASH_BufferFastRead(buffer,address24,length);
}

void fy_flash_erase(){
	SPI_Flash_ChipEaser();

}
void fy_flash_erase_block(uint32_t start,uint32_t sizeOfBitmaps){
   SpiFlash_EraseSizeBlock(start,sizeOfBitmaps);
   //SPI_Flash_ChipEaser();
}
void fy_flash_page_write(uint32_t addr,uint8_t* data,uint32_t size){
	SPI_FLASH_BufferWrite(data,addr,size);
}

uint32_t fy_flash_isreceiveing(void)
{
	if(spi_dma_finish){
		return true;
	}
}

void fy_flash_waitreceive_done(void)
{
	while(spi_dma_finish == 0){
		__NOP();
	}
	return;
}
void fy_flash_deinit(void)
{
}
const flythings_flash	m_flash ={
  .flash_init = fly_flash_init,
  .flash_deinit = fy_flash_deinit,
  .flash_readdata = fy_flash_read,
  .flash_readdata_dma = fy_flash_readdma,
  .flash_wait_dma_end = fy_flash_waitreceive_done, 
  .flash_isreading = fy_flash_isreceiveing
};